<?php
// 处理登录功能

// 获取 用户输入的验证码
$userCode = $_REQUEST["userCode"];
// 获取系统生成的验证码字符串
$code = $_SESSION['code'];
if($userCode != $code){
    header("location:http://localhost/shop/loginPage.php?msg=验证码输入错误");
    return;
}
// echo "$userCode<br>$code";

$loginName = $_REQUEST["loginName"];
$password = $_REQUEST["password"];

$sql = "select u_id,u_login_name,u_password,u_image_url,u_phone from t_user where u_login_name = '{$loginName}' and u_password = password('{$password}')";

require_once("util/DButil.php");
$userArr = query($sql);
if($userArr){
    // 用户名密码正确, 登录成功
    $_SESSION["user"] = $userArr[0];
    // 初始化购物车
    $_SESSION["cart"] = [];
    header("location:http://localhost/shop/index.php");
} else {
    header("location:http://localhost/shop/loginPage.php?msg=用户名或密码错误");
}

?>